﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DullBoard.Data;
using DullBoard.Model;

namespace DullBoard
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

            var repo = new ForumRepository();
            var forums = repo.GetForums();

            foreach(var forum in (from p in forums
                                select new {p.CategoryId,p.CategoryName}).Distinct())
            {
                var parentMenuItem = new ToolStripMenuItem() {Text = forum.CategoryName, Tag = forum.CategoryId};

                mnuForums.DropDownItems.Add(parentMenuItem);

                foreach(var f in (from c in forums
                                    where c.CategoryId == forum.CategoryId
                                      select c))
                {
                    var childMenuItem = new ToolStripMenuItem() { Text = f.Name, Tag = f.Id,ToolTipText = f.Description};
                    childMenuItem.Click += ForumLinkClick;
                    parentMenuItem.DropDownItems.Add(childMenuItem);
                }
            }


        }

        private void ForumLinkClick(object sender, EventArgs e)
        {
            var menuItem = (ToolStripMenuItem) sender;
            var forumId = Int64.Parse(menuItem.Tag.ToString());

            RenderForumOverview(forumId);
        }

        private void SettingsToolStripMenuItemClick(object sender, EventArgs e)
        {
            var settings = new frmSettings();
            settings.ShowDialog();
        }

        private void RenderForumOverview(long forumId)
        {
            var repo = new ForumRepository();
            var bob = repo.GetForum(forumId);

            gridForums.DataSource = bob.Topics;

            gridForums.Columns["ContainsBlizzardPost"].DisplayIndex = 1;
            gridForums.Columns["Sticky"].DisplayIndex = 2;
            gridForums.Columns["Title"].DisplayIndex = 3;
            gridForums.Columns["Author"].DisplayIndex = 4;
            gridForums.Columns["DatePosted"].DisplayIndex = 5;
            gridForums.Columns["PageCount"].Visible = false;
            gridForums.Columns["ReplyCount"].HeaderText = "Replies";
            gridForums.Columns["ViewCount"].HeaderText = "Viewed";
            gridForums.Columns["ContainsBlizzardPost"].HeaderText = "Blue";
            gridForums.Columns["BlizzardPost"].Visible = false;
            gridForums.Columns["DatePosted"].ValueType = typeof (DateTime);

            gridForums.Columns["Id"].Visible = false;
            gridForums.Columns["Summary"].Visible = false;

            gridForums.Show();
        }

        private void GridForumsCellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var topics = (List<Topic>)gridForums.DataSource;
            var topic = topics[e.RowIndex];

            var threadForm = new frmThread(topic);
            threadForm.ShowDialog();

        }
    }
}
